package com.my.aslife.aop;


import com.my.aslife.exception.BusinessException;
import com.my.aslife.module.common.ErrorCode;
import com.my.aslife.module.common.IPThreadLocal;
import com.my.aslife.module.common.UserIdThreadLocal;
import com.my.aslife.module.entity.User;
import com.my.aslife.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author mayuan
 * @description TODO
 */
@Component
@Aspect
@Slf4j
@Order(Integer.MAX_VALUE-1)
public class LogAOP {



    @Around("execution(* com.my.aslife.controller..*(..))")
    public Object loglog(ProceedingJoinPoint pjp) throws Throwable {

        log.info(">==<");
        Object[] args = pjp.getArgs();
        for(int i = 0;i < args.length;i++){
            // 请求参数打印
            log.info("参数 "+(i+1)+" : {}",args[i]);
        }

        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(
                RequestContextHolder.getRequestAttributes())).getRequest();
        // 记录请求 IP

        log.info("请求路径：{}", request.getRequestURI());
        log.info(">==<");

        Object result = pjp.proceed();
        return result;
    }
}
